<template>
  <div :class="className" :style="{ height: height, width: width, minWidth: width }" />
</template>

<script>

// import resize from './mixins/resize'

import * as echarts from 'echarts';
require("echarts/theme/macarons");

export default {
  // mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'echarts-echartCard'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '320px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    },
    sName: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        console.log(val)
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ xData, sData } = {}) {
      let _this = this
      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {},
        grid: {
          left: 10,
          right: 24,
          bottom: 20,
          top: 15,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          renderMode: 'html',
          backgroundColor: '#F4F6FB',
          formatter(params) {
            const [firstElement] = params
            return `<div class="echartCard">
            <p class="echartCard-title">${firstElement.axisValueLabel}</p>
            <div class="echartCard-panel"><span>${_this.sName}</span><span class="echartCard-value">${(
                Number(firstElement.value)
              ).toLocaleString()}</span></div>
          </div>`
          },
          className: 'echarts-tooltip echarts-tooltip-dark'
        },
        xAxis: {
          type: 'value',
          minInterval:1,
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            color: '#9DA6AE'
          }
        },
        yAxis: {
          axisLabel: {
            color: '#9DA6AE'
          },
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          splitLine: {
            show: false,
          },
          splitTick: {
            show: false,
          },
          splitArea: {
            show: false
          },
          data: sData
        },
        series: [
          {
            name: '',
            type: 'bar',
            barWidth: '20',
            data: xData,
            itemStyle: {
              normal: {
                color: {
                  type: 'linear',
                  x: 0,
                  y: 1,
                  x2: 1,
                  y2: 0,
                  colorStops: [{
                    offset: 0, color: _this.chartData.type == 1 ? '#FFB36B' : '#94E2A8' // 0% 处的颜色
                  }, {
                    offset: 1, color: _this.chartData.type == 1 ? '#FF924E' : '#7BCF95'  // 100% 处的颜色
                  }],
                  globalCoord: false
                }
              }
            }
          }
        ]
      }, true)
    }
  }
}
</script>
